//
// Created by YimingCai on 26/03/2017.
//
#include <cmath>
#include <stdint.h>

class Solution {
public:
    int reverse(int x) {
        if (x == 0)
            return 0;
        bool isNegative = (x < 0);
        int64_t x64 = x;
        if (x < 0)
            x64 = - x64;
        int count = 0;
        while (! (x64 % 10))
            x64 /= 10;
        int64_t ret = 0;
        do {
            ret = ret * 10 + x64 % 10;
            x64 /= 10;
        } while (x64);
        if (ret > INT32_MAX) {
            ret = 0;
        }
        if (isNegative) ret = -ret;
        return int(ret);
    }
};


#include <gtest/gtest.h>
TEST(ReverseIntegerTest, test1)
{
    Solution s = Solution();
    EXPECT_EQ(0, s.reverse(0));
    EXPECT_EQ(321, s.reverse(123));
    EXPECT_EQ(-321, s.reverse(-123));
    EXPECT_EQ(1, s.reverse(10));
    EXPECT_EQ(1, s.reverse(100));
    EXPECT_EQ(0, s.reverse(1000000003));
    EXPECT_EQ(0, s.reverse(-2147483648));
}

int main(int argc, char** argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}
